package com.seanzx.server;

import com.seanzx.boot.BootLoader;
import com.seanzx.server.handler.ConnectionHandler;

import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.*;

import static com.seanzx.server.common.Constant.*;

public class SeanDBServer {
    private static final ExecutorService EXECUTOR_SERVICE = new ThreadPoolExecutor(MIN_POOL_SIZE, MAX_POOL_SIZE,
                                                            KEEP_ALIVE_TIME, TimeUnit.SECONDS, new ArrayBlockingQueue<>(QUEUE_SIZE),
                                                            r -> {
                                                                Thread t = new Thread(r);
                                                                t.setName("sean-db-connection-pool-" + Thread.currentThread().getId());
                                                                return t;
                                                            });

    public static void main(String[] args) {

        try {
            new BootLoader().load();
            ServerSocket serverSocket = new ServerSocket(13306);
            Socket socket;

            while (true) {
                socket = serverSocket.accept();
                ConnectionHandler connectionHandler = new ConnectionHandler(socket);
                EXECUTOR_SERVICE.submit(connectionHandler);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
